<?php

namespace app\admin\controller;

use think\Db;

class City
{
    public function __construct()
    {
        Admin_auth();
    }

    public function index()
    {
//		$type = input("get.type")?input("get.type"):'image';
        if (session('type') == 'all') {
            $data = Db::table("province")->order('id asc')->select();
        } else {
            $data = Db::table("province")->whereIn('id', session('aid'))->order('id asc')->select();
        }
        return view('', ['data' => $data]);
    }

    public function ajax_get()
    {
        $type = input('get.type');
        $father_id = input('get.id');
        $nofather = request()->request('nofather');
        $where = [];
        if (!empty($father_id)) {
            if ($nofather == 'ok') {
                $where['id'] = $father_id;
            } else {
                $where['father_id'] = $father_id;
            }
        }
        if($type != 'province'){
            if($father_id == '')
                die;
        }
        if (session('type') == 'all') {
            $data = Db::table($type)->where($where)->order('id asc')->select();
        } else {
            if ($type != 'area') {
                $where = [];
                $where['status'] = 1;
            }
            $data = Db::table($type)->whereIn('id', session('aid'))->where($where)->order('id asc')->select();
        }
        return json(['data' => $data]);
    }

    public function status()
    {
        $type = input('get.type');
        $id = input('get.id');
        $status = input('get.status');
        switch ($type)
        {
            case 'province':
                if(session('type') != 'all'){
                    if(!in_array($id,explode(',',session('aid')))){
                        break;
                    }
                }
                Db::table($type)->where(['id'=>$id])->setField('status',$status);
                Db::table('city')->where(['father_id'=>$id])->setField('status',$status);
                $city = Db::table('city')->where(['father_id'=>$id])->column('id');
                Db::table('area')->whereIn('father_id',$city)->setField('status',$status);
                break;
            case 'city':
                if(session('type') != 'all'){
                    if(!in_array($id,explode(',',session('aid')))){
                        break;
                    }
                }
                Db::table($type)->where(['id'=>$id])->setField('status',$status);
                Db::table('area')->where(['father_id'=>$id])->setField('status',$status);
                break;
            case 'area':
                if(session('type') != 'all'){
                    if(!in_array($id,explode(',',session('aid')))){
                        break;
                    }
                }
                Db::table($type)->where(['id'=>$id])->setField('status',$status);
                break;
            default:
                echo "No number between 1 and 3";
        }
        redirect(url('/admin/city'));
        exit;
    }
}
